// pages/musicPlayer/musicPlayer.js
import {getSongDetail, getSongLyric} from '../../service/request/player.js'
import songListStore from '../../store/songList.js'
import {formatLyrics} from '../../utils/formatLyrics'
const audioContext = wx.createInnerAudioContext()
const orderName = ["order", "repeat", "random"]
Page({
  data: {
    id: 0,
    songDetail: {},
    songList: [],
    songIndex: 0,
    lyrics: "",
    lyricsList: {},
    currentIndex: -1,
    currentLyrics: "",
    titleConfig: ["歌曲", "歌词"],
    currentPage: 0,
    contentHeight: 100,
    currentTime: 0,
    durationTime: 0,
    sliderLength: 0,
    scrollToTop: 0,
    isSlide: false,
    isPlaying: true,
    currentOrder: 0,
    currentOrderName: "order"
  },
  onLoad(options) {
    const id = options.id
    // 获取内容高度
    const app = getApp()
    const contentHeight = app.globalData.contentHeight
    this.setData({contentHeight})
    // 获取歌曲列表
    songListStore.onStates(["list", "index"],this.handleSongsList)
    // 获取歌曲
    this.getSong(id)
    audioContext.onEnded(this.nextClick)
  },
  // store操作
  handleSongsList({list, index}) {
    if(list) {
      this.setData({songList: list})
    }
    if(index !== undefined) {
      this.setData({songIndex: index})
    }
  },
  // 监听页面切换
  pageChange(event) {
    const currentPage = event.detail.current
    this.setData({currentPage})
  },
  // 点击页面切换
  pageChangeClick(event) {
    const currentPage = event.currentTarget.dataset.currentpage
    this.setData({currentPage})
  },
   // 歌曲进度条切换
   sliderChange(event) {
      const currentNewTime = event.detail.value / 100 * this.data.durationTime
      audioContext.seek(currentNewTime / 1000)
      this.setData({currentTime: currentNewTime, isSlide: false})
      
   },
  //  滑动进度条
  sliderChanging(event) {
    this.data.isSlide = true
    // const currentNewTime = event.detail.value / 100 * this.data.durationTime
    // this.setData({currentTime: currentNewTime})
  },
  // 播放暂停切换
  playClick() {
    if(this.data.isPlaying) {
      audioContext.pause()
      this.setData({isPlaying: false})
    } else {
      audioContext.play()
      this.setData({isPlaying: true})
    }
  },
  // 下一首
  nextClick() {
    this.indexChange()
  },
  // 上一首
  prevClick() {
    this.indexChange(false)
  },
  //歌曲跳转
  indexChange(next = true) {
    let currentIndex = this.data.songIndex
    const length = this.data.songList.length
    switch(this.data.currentOrder) {
      case 0:
        currentIndex = next ?  currentIndex + 1 : currentIndex - 1
        currentIndex = (currentIndex + length) % length
        break;
      case 1:
        break;
      case 2:
        currentIndex = Math.floor(Math.random() * length)
        break;
    }
    
    songListStore.setState("index", currentIndex)
    const currentId = this.data.songList[currentIndex].id
    this.getSong(currentId)
  },
  //获取歌曲
  getSong(id) {
    this.setData({id})
    // 获取歌曲详情
    getSongDetail(id).then(res => {
      const songDetail = res.songs[0]
      const durationTime = res.songs[0].dt
      this.setData({songDetail})
      this.setData({durationTime})

    })
    // 获取歌词
    getSongLyric(id).then(res => {
      this.setData({lyrics: res.lrc.lyric})
      const lyricsList = formatLyrics(this.data.lyrics)
      this.setData({lyricsList})
      // console.log(lyricsList);
    })
    // 歌曲播放
    audioContext.stop()
    audioContext.src = `https://music.163.com/song/media/outer/url?id=${id}.mp3`
    audioContext.autoplay = true
    audioContext.play()
    audioContext.onTimeUpdate(() => {
      if(this.data.isSlide) return
      const currentTime = audioContext.currentTime * 1000
      this.setData({currentTime})
      const sliderLength = this.data.currentTime / this.data.durationTime * 100
      this.setData({sliderLength})
      // 切换歌词
      const lyricsList = this.data.lyricsList
      let index = lyricsList.length - 1
      for(let i = 0; i < lyricsList.length; i++) {
        if(lyricsList[i].time > audioContext.currentTime * 1000) {
          index = i - 1
          break
        }
      }
      if(this.data.currentIndex === index) return 
      this.setData({currentIndex: index})
      const currentLyrics = lyricsList[index].text
      this.setData({currentLyrics, scrollToTop: 40 * index})
    })
  },
  // 切换模式
  orderChange() {
    let currentOrder = this.data.currentOrder
    currentOrder = (currentOrder + 1) % 3
    this.setData({currentOrder, currentOrderName: orderName[currentOrder]})
  }
})